package com.example.demo.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.demo.common.Constants;
import com.example.demo.common.Result;
import com.example.demo.entity.Dict;
import com.example.demo.entity.Menu;
import com.example.demo.entity.Role;
import com.example.demo.entity.User;
import com.example.demo.mapper.DictMapper;
import com.example.demo.service.impl.MenuServiceImpl;
import com.example.demo.service.impl.RoleServiceImpl;
import lombok.NonNull;
import org.springframework.web.bind.annotation.*;
import org.springframework.stereotype.Controller;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author wzz
 * @since 2023-09-23
 */
@RestController
@RequestMapping("/menu")
public class MenuController {
    @Resource
    private MenuServiceImpl menuService;

    @Resource
    private DictMapper dictMapper;
    @PostMapping("/add")
    public Result add(@RequestBody Menu menu){
        return Result.success(menuService.saveUser(menu));
    }
    @GetMapping("/list")
    public Result findPage(@RequestParam Integer pageNum, @RequestParam Integer pageSize){
        IPage<Menu> page = new Page<>(pageNum,pageSize);
        QueryWrapper queryWrapper = new QueryWrapper<>();
        return Result.success(menuService.page(page,queryWrapper));
    }

    @GetMapping("/all_list")
    public Result findAll(){
        return Result.success(menuService.findMenus());
    }

    @GetMapping("/byPid/{id}")

    public Result getByPid(@PathVariable String id){
        return Result.success(menuService.getDataById(id));
    }

    @GetMapping("/{id}")
    public Result findOne(@PathVariable String id){
        return Result.success(menuService.getById(id));
    }
    @DeleteMapping("/del/{id}")
    public Result deleteRole(@PathVariable String id){
        return Result.success(menuService.removeById(id));
    }

    @GetMapping("/icons")
    public Result getIcons(){
        QueryWrapper<Dict> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("type", Constants.DICT_TYPE);
        List<Dict> list = dictMapper.selectList(queryWrapper);
        return Result.success(list);
    }
}
